/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import controller.ConverterFacade;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.inject.Named;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.ejb.EJB;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject;
import model.Currency;

/**
 *
 * @author yann
 */
@Named(value = "converterManager")
@ConversationScoped
public class converterManager implements Serializable {

    public static final int EUR = 0;
    public static final int SEK = 1;
    public static final int DOLLAR = 2;
    @EJB
    private ConverterFacade converterFacade;
    private Exception transactionFailure;
    private float amount;
    private String from;
    private String to;
    private double converted = -1;
    private Vector<String> test;
    @Inject
    private Conversation conversation;
    private List storedCurrencies;

    private void startConversation() {
        if (conversation.isTransient()) {
            conversation.begin();
        }
    }

    private void stopConversation() {
        if (!conversation.isTransient()) {
            conversation.end();
        }
    }
    
    private void handleException(Exception e) {
        stopConversation();
        e.printStackTrace(System.err);
        transactionFailure = e;
    }
    /**
     * Returns the latest thrown exception.
     */
    public Exception getException() {
        return transactionFailure;
    }
    
    /**
     * Returns <code>false</code> if the latest transaction failed, otherwise <code>false</code>.
     */
    public boolean getSuccess() {
        return transactionFailure == null;
    }

    /** Creates a new instance of converterManager */
    public converterManager() {
        test = new Vector();
        String one = new String("euro");
        String two = new String("SEK");
        test.add(one);
        test.add(two);
    }

    public String getTest() {
        converterFacade.setCurrencies();
        return "tut";

    }

    public float getAmount() {
        return amount;
    }

    public double getConverted() {
        return converted;
    }

    public void setAmount(float amount) {
        this.amount = amount;
    }

    public String getFrom() {
        System.out.println("getfrom");
        return from;
    }

    public String getTo() {
        return to;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public void setTo(String to) {
        this.to = to;
    }

    public void convert() {
        try {
            startConversation();
            //converterFacade.storeCurrencies();
            System.out.println("Push convert: ");
            System.out.println("Amount: " + amount);
            System.out.println("From: " + from);
            System.out.println("to: " + to);
            // call EJB here to do the conversion and set the value in converted
            converterFacade.convert(amount, from, to);
            converted = converterFacade.convertedAmount();

            System.out.println("Converted: " + converted);
        } catch (Exception ex) {
            handleException(ex);
        }
    }

    public void setStoredCurrencies(List<String> list) {
        this.storedCurrencies = list;
    }

    public List getStoredCurrencies() {
        List<Currency> currency = converterFacade.getStoredCurrencies();
        List strCur = new ArrayList(); 
        for (Currency cur : currency) { 
            strCur.add(cur.getFromCurrency());
        }
        return strCur;
    }
    
    public String errorCurrencyEmpty(){
        return "Amount field must contain a value";
    }
}
